import { createRouter, createWebHistory } from 'vue-router'
import Layout from '../layout/index.vue'

const routes = [
    {
        path: '/login',
        name: 'Login',
        component: () => import('@/views/login/index.vue'),
        meta: {
            title: '登录'
        }
    },
    {
        path: '/',
        component: Layout,
        redirect: '/dashboard',
        children: [
            {
                path: 'dashboard',
                name: 'dashboard',
                component: () => import('../views/DashboardView.vue'),
                meta: {
                    title: '仪表盘',
                    requiresAuth: true
                }
            },
            {
                path: '/task',
                name: 'task',
                component: () => import('../views/task/index.vue'),
                redirect: '/task/schedule',
                meta: {
                    title: '任务管理',
                    requiresAuth: true
                },
                children: [
                    {
                        path: 'schedule',
                        name: 'schedule',
                        component: () => import('../views/task/schedule/index.vue'),
                        meta: {
                            title: '定时任务',
                            requiresAuth: true
                        }
                    },
                    {
                        path: 'log',
                        name: 'taskLog',
                        component: () => import('../views/task/log/index.vue'),
                        meta: {
                            title: '任务日志',
                            requiresAuth: true
                        }
                    }
                ]
            },
            {
                path: 'profile',
                name: 'profile',
                component: () => import('../views/profile/index.vue'),
                meta: {
                    title: '个人信息',
                    requiresAuth: true
                }
            },
            {
                path: 'change-password',
                name: 'changePassword',
                component: () => import('../views/change-password/index.vue'),
                meta: {
                    title: '修改密码',
                    requiresAuth: true
                }
            },
            {
                path: '/schedule',
                name: 'Schedule',
                component: () => import('../views/schedule/index.vue'),
                meta: {
                    title: '日程管理',
                    icon: 'Calendar',
                    requiresAuth: true
                }
            }
        ]
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
    const token = localStorage.getItem('token')

    if (to.path === '/login') {
        if (token) {
            next('/dashboard')
        } else {
            next()
        }
    } else {
        if (token) {
            next()
        } else {
            next('/login')
        }
    }
})

export default router 